package com.lcz.googleplay.holder;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lcz.googleplay.R;
import com.lcz.googleplay.config.BaseProtocol;
import com.lcz.googleplay.domain.DetailInfo;
import com.lcz.googleplay.utils.UIUtils;
import com.yolanda.nohttp.rest.Response;
import com.yolanda.nohttp.rest.SimpleResponseListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by hp on 2016/12/8.
 */

public class Detail_SafeHolder extends BaseHolder<DetailInfo> {
    @BindView(R.id.iv_1)
    ImageView iv1;
    @BindView(R.id.iv_2)
    ImageView iv2;
    @BindView(R.id.iv_3)
    ImageView iv3;
    @BindView(R.id.iv_4)
    ImageView iv4;
    @BindView(R.id.safe_title)
    LinearLayout safeTitle;
    @BindView(R.id.safe_arrow)
    ImageView safeArrow;
    @BindView(R.id.safe_title_layout)
    RelativeLayout safeTitleLayout;
    @BindView(R.id.des_iv_1)
    ImageView desIv1;
    @BindView(R.id.des_tv_1)
    TextView desTv1;
    @BindView(R.id.des_layout_1)
    LinearLayout desLayout1;
    @BindView(R.id.des_iv_2)
    ImageView desIv2;
    @BindView(R.id.des_tv_2)
    TextView desTv2;
    @BindView(R.id.des_layout_2)
    LinearLayout desLayout2;
    @BindView(R.id.des_iv_3)
    ImageView desIv3;
    @BindView(R.id.des_tv_3)
    TextView desTv3;
    @BindView(R.id.des_layout_3)
    LinearLayout desLayout3;
    @BindView(R.id.des_iv_4)
    ImageView desIv4;
    @BindView(R.id.des_tv_4)
    TextView desTv4;
    @BindView(R.id.des_layout_4)
    LinearLayout desLayout4;
    @BindView(R.id.safe_content)
    LinearLayout safeContent;
    @BindView(R.id.safe_layout)
    RelativeLayout safeLayout;

    ImageView[] ivs;//标题栏图片
    ImageView[] iv_des;//条目描述图片
    TextView[] tv_des;//条目描述文本
    LinearLayout[] des_layou;//条目布局


    @Override
    public View init() {
        View view = View.inflate(UIUtils.getActivity(), R.layout.detail_safe, null);
        ButterKnife.bind(this, view);
        //初始化标题栏图片
        ivs = new ImageView[4];
        ivs[0] = iv1;
        ivs[1] = iv2;
        ivs[2] = iv3;
        ivs[3] = iv4;
        //初始化条目描述图片
        iv_des = new ImageView[4];
        iv_des[0] = desIv1;
        iv_des[1] = desIv2;
        iv_des[2] = desIv3;
        iv_des[3] = desIv4;
        //初始化条目描述文本
        tv_des = new TextView[4];
        tv_des[0] = desTv1;
        tv_des[1] = desTv2;
        tv_des[2] = desTv3;
        tv_des[3] = desTv4;
        //初始化条目布局
        des_layou = new LinearLayout[4];
        des_layou[0] = desLayout1;
        des_layou[1] = desLayout2;
        des_layou[2] = desLayout3;
        des_layou[3] = desLayout4;
        return view;
    }

    @Override
    public void refreshView(DetailInfo data) {
        //获取数据
        List<DetailInfo.SafeBean> dataSafe = data.getSafe();
        List<String> safeUrl = new ArrayList<>();
        List<String> safeDes = new ArrayList<>();
        List<String> safeDesUrl = new ArrayList<>();
        List<Integer> safeDesColor = new ArrayList<>();
        for (DetailInfo.SafeBean dataSafeList : dataSafe) {
            safeUrl.add(BaseProtocol.IMAGE_URL + dataSafeList.getSafeUrl());
            safeDes.add(dataSafeList.getSafeDes());
            safeDesUrl.add(BaseProtocol.IMAGE_URL + dataSafeList.getSafeDesUrl());
            safeDesColor.add(dataSafeList.getSafeDesColor());
        }

        //根据数据大小显示(这里最大是4)
        for (int i = 0; i < dataSafe.size(); i++) {
            if (i < dataSafe.size()) {
                ivs[i].setVisibility(View.VISIBLE);
                des_layou[i].setVisibility(View.VISIBLE);
                final int finalI = i;
                BaseProtocol.loadImage(BaseProtocol.IMAGE_URL + dataSafe.get(i).getSafeUrl(), new SimpleResponseListener<Bitmap>() {
                    @Override
                    public void onSucceed(int what, Response<Bitmap> response) {
                        super.onSucceed(what, response);
                        ivs[finalI].setImageBitmap(response.get());
                    }
                });

                BaseProtocol.loadImage(BaseProtocol.IMAGE_URL + dataSafe.get(i).getSafeDesUrl(), new SimpleResponseListener<Bitmap>() {
                    @Override
                    public void onSucceed(int what, Response<Bitmap> response) {
                        super.onSucceed(what, response);
                        iv_des[finalI].setImageBitmap(response.get());
                    }
                });

                tv_des[i].setText(safeDes.get(i));

                //根据服务器返回数据显示不同颜色
                int currentColor = dataSafe.get(i).getSafeDesColor();
                int color;
                if (currentColor >= 1 && currentColor <= 3) {
                    color = Color.rgb(255, 153, 0);
                } else if (currentColor == 4) {
                    color = Color.rgb(0, 177, 62);
                } else {
                    color = Color.rgb(122, 122, 122);
                }

                tv_des[i].setTextColor(color);
            } else {
                ivs[i].setVisibility(View.GONE);
                des_layou[i].setVisibility(View.GONE);
            }
        }


    }


    @OnClick({R.id.safe_content, R.id.safe_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.safe_content:
                break;
            case R.id.safe_layout:
                break;
        }
    }
}
